package com.immunology.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.immunology.model.ComplaintType;
import com.immunology.service.ComplaintTypeService;

public class GetComplaintTypeListBySyndromeId extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public GetComplaintTypeListBySyndromeId() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		int syndromeId = Integer.parseInt(request.getParameter("syndromeId"));

		List<ComplaintType> complaintTypeList = null;
		ComplaintTypeService complaintTypeService = ComplaintTypeService
				.getInstance();
		complaintTypeList = complaintTypeService
				.getComplaintTypeListBySyndromeId(syndromeId);

		JSONArray complaintTypeObjectList = new JSONArray();
		JSONObject complaintTypeObject;
		for (ComplaintType complaintType : complaintTypeList) {
			complaintTypeObject = new JSONObject();
			complaintTypeObject.put("id", new Integer(complaintType.getId()));
			complaintTypeObject
					.put("name", new String(complaintType.getName()));
			complaintTypeObjectList.add(complaintTypeObject);
		}

		response.setContentType("text/plain");
		response.setCharacterEncoding("UTF-8");
		PrintWriter writer = response.getWriter();
		System.out.println(complaintTypeObjectList);
		writer.print(complaintTypeObjectList);
	}

}
